package com.freewater.springboot.demo.spel.beanref;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

public class Main {
    public static void main(String[] args) {

        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
        applicationContext.registerShutdownHook();

        StandardEvaluationContext context = new StandardEvaluationContext();
        context.setBeanResolver(new BeanFactoryResolver(applicationContext));

        ExpressionParser parser = new SpelExpressionParser();
        final AppConfig.MyBean myBean = parser.parseExpression("@myBean").getValue(context, AppConfig.MyBean.class);
        assert myBean != null;
        System.out.println(myBean.getName()); // freewater

        // get the FactoryBean
        final AppConfig.MyBeanFactory myBeanFactory = parser.parseExpression("&myBean").getValue(context, AppConfig.MyBeanFactory.class);
        System.out.println(myBeanFactory);
    }
}
